<template>
  <div class="home page-component">
    <el-carousel indicator-position="outside">
      <el-carousel-item v-for="item in 2" :key="item">
        <img src="~assets/images/web-banner1.png" alt="" />
      </el-carousel-item>
    </el-carousel>
    <!-- 搜索 -->
    <div class="search-container">
      <div class="search-wrapper">
        <div class="hospital-search">
          <el-autocomplete
            class="search-input"
            prefix-icon="el-icon-search"
            v-model="hosname"
            :fetch-suggestions="querySearchAsync"
            :trigger-on-focus="false"
            placeholder="点击输入医院名称"
            @select="handleSelect"
          >
            <span
              slot="suffix"
              class="search-btn v-link highlight clickable selected"
              >搜索
            </span>
          </el-autocomplete>
        </div>
      </div>
    </div>
    <!-- bottom -->
    <div class="bottom">
      <div class="left">
        <div class="home-filter-wrapper">
          <div class="title">医院</div>
          <div>
            <div class="filter-wrapper">
              <span class="label">等级：</span>
              <div class="condition-wrapper">
                <span
                  v-for="(item, index) in hostypeList"
                  :key="index"
                  :class="hostypeActiveIndex == index ? 'selected' : ''"
                  @click="hostypeSelect(item.value, index)"
                  class="item v-link clickable"
                >
                  {{ item.name }}
                </span>
              </div>
            </div>
            <div class="filter-wrapper">
              <span class="label">地区：</span>
              <div class="condition-wrapper">
                <span
                  v-for="(item, index) in districtList"
                  :key="index"
                  :class="provinceActiveIndex == index ? 'selected' : ''"
                  @click="districtSelect(item.value, index)"
                  class="item v-link clickable"
                >
                  {{ item.name }}
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="v-scroll-list hospital-list">
          <div
            v-for="(item, index) in list"
            :key="index"
            class="v-card clickable list-item"
            @click="show(item.hoscode)"
          >
            <div class="">
              <div class="hospital-list-item hos-item" index="0">
                <div class="wrapper">
                  <div class="hospital-title">{{ item.hosname }}</div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      {{ item.param.hostypeString }}
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      每天{{ item.bookingRule.releaseTime }}放号
                    </div>
                  </div>
                </div>
                <img
                  :src="'data:image/jpeg;base64,' + item.logoData"
                  :alt="item.hosname"
                  class="hospital-img"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="common-dept">
          <div class="header-wrapper">
            <div class="title">常见科室</div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <span class="item v-link clickable dark">神经内科 </span>
            <span class="item v-link clickable dark">消化内科 </span>
            <span class="item v-link clickable dark">呼吸内科 </span>
            <span class="item v-link clickable dark">内科 </span>
            <span class="item v-link clickable dark">神经外科 </span>
            <span class="item v-link clickable dark">妇科 </span>
            <span class="item v-link clickable dark"> 产科 </span>
            <span class="item v-link clickable dark">儿科 </span>
          </div>
        </div>
        <div class="space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper">
                <span class="iconfont title-icon"></span>
              </div>
              <span class="title">平台公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"
                >关于延长北京大学国际医院放假的通知
              </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"
                >北京中医药大学东方医院部分科室医生门诊医
              </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">
                武警总医院号源暂停更新通知
              </span>
            </div>
          </div>
        </div>
        <div class="suspend-notice-list space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper">
                <span class="iconfont title-icon"></span>
              </div>
              <span class="title">停诊公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">
                中国人民解放军总医院第六医学中心(原海军总医院)呼吸内科门诊停诊公告
              </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">
                首都医科大学附属北京潞河医院老年医学科门诊停诊公告
              </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"
                >中日友好医院中西医结合心内科门诊停诊公告
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import hospApi from "@/api/hosp";
import dictApi from "@/api/dict";

export default {
  // 服务端渲染，显示医院列表
  asyncData({ params, error }) {
    return hospApi.getPageList(1, 10, null).then((res) => {
      return {
        list: res.data.content,
        pages: res.data.totalPages,
      };
    });
  },
  data() {
    return {
      searchObj: {},
      page: 1,
      limit: 10,

      hosname: "", //医院名称
      hostypeList: [{ name: "全部", value: "" }], //医院等级集合
      districtList: [{ name: "全部", value: "" }], //地区集合

      hostypeActiveIndex: 0,
      provinceActiveIndex: 0,
    };
  },
  methods: {
    init() {
      // 查询医院等级
      dictApi.findByDictCode("Hostype").then((res) => {
        // 先清空列表，同时放入”全部“
        this.hostypeList = [{ name: "全部", value: "" }];

        const dictList = res.data;
        if (dictList.length > 0) {
          dictList.forEach((dict) => {
            this.hostypeList.push(dict);
          });
        }
      });
      // 查询地区
      dictApi.findByDictCode("Beijing").then((res) => {
        this.districtList = [{ name: "全部", value: "" }];

        const dictList = res.data;
        if (dictList.length > 0) {
          dictList.forEach((dict) => {
            this.districtList.push(dict);
          });
        }
      });
    },
    // 查询医院列表
    getHospList(hosname) {
      if (hosname) {
        this.searchObj.hosname = this.hosname;
      } else {
        this.searchObj.hosname = null;
      }
      hospApi.getPageList(this.page, this.limit, this.searchObj).then((res) => {
        this.list = [];
        const list = res.data.content;
        list.forEach((hosp) => {
          this.list.push(hosp);
        });
      });
    },

    hostypeSelect(hostype, index) {
      // 准备数据
      this.list = [];
      this.page = 1;
      this.hostypeActiveIndex = index;
      this.searchObj.hostype = hostype;

      this.getHospList(this.hosname);
    },

    districtSelect(districtCode, index) {
      // 准备数据
      this.list = [];
      this.page = 1;
      this.provinceActiveIndex = index;
      this.searchObj.districtCode = districtCode;

      this.getHospList(this.hosname);
    },
    // 输入框输入值弹出下拉框显示相关内容
    querySearchAsync(queryString, cb) {
      this.searchObj = {};
      if (queryString == "" || queryString == null) return;
      hospApi.getByHosname(queryString).then((res) => {
        res.data.forEach((hosp) => {
          hosp.value = hosp.hosname;
        });
        cb(res.data);
      });
    },
    // 在下拉框选择某一内容执行下面方法
    handleSelect(item) {
      window.location.href = "/hospital/" + item.hoscode;
    },
    show(hoscode) {
      window.location.href = "/hospital/" + hoscode;
    },
    load() {
      this.page += 1;
      this.getHospList(this.hosname);
    },
  },

  created() {
    this.init();
  },
};
</script>
